﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ManageTicket.Library.Control
{
    public partial class ShowNumGrid : DataGridView
    {
        public ShowNumGrid():base()
        {
            this.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(ShowNumGrid_EditingControlShowing);
        }

        void ShowNumGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            try
            {
                if (this.CurrentCell.ValueType == typeof(int))
                {
                    TextBox textboxcell = (TextBox)e.Control;
                    if (textboxcell != null)
                    {
                        textboxcell.KeyPress += new KeyPressEventHandler(textboxcell_KeyPress);
                    }
                }
                else if (this.CurrentCell.ValueType == typeof(double))
                {
                    TextBox textboxcellDouble = (TextBox)e.Control;
                    if (textboxcellDouble != null)
                    {
                        textboxcellDouble.KeyPress += new KeyPressEventHandler(textboxcellDouble_KeyPress);
                    }
                }
                else if(this.CurrentCell.ValueType == typeof(string))
                {
                    TextBox textboxcellNormal = (TextBox)e.Control;
                    if (textboxcellNormal != null)
                    {
                        textboxcellNormal.KeyPress += new KeyPressEventHandler(textboxcellNormal_KeyPress);
                    }
                }
            }
            catch (Exception ex)
            {
                
                //throw ex;
            }
            
        }

        void textboxcellNormal_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = false;
        }

        void textboxcellDouble_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !(e.KeyChar.ToString() == "\b"))
            {
                if ((e.KeyChar.ToString() == "."))
                {
                    if (((TextBox)sender).Text.IndexOf(".") != -1)
                    {
                        e.Handled = true;
                    }
                    else
                        e.Handled = false;
                }
                else
                    e.Handled = true;
            }
            else
                e.Handled = false;
        }

        void textboxcell_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !(e.KeyChar.ToString() == "\b"))
                e.Handled = true;
            else
                e.Handled = false;
        }

        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            //store a string representation of the row number in 'strRowNumber'
            string strRowNumber = (e.RowIndex + 1).ToString();

            //prepend leading zeros to the string if necessary to improve
            //appearance. For example, if there are ten rows in the grid,
            //row seven will be numbered as "07" instead of "7". Similarly, if 
            //there are 100 rows in the grid, row seven will be numbered as "007".
            while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;

            //determine the display size of the row number string using
            //the DataGridView's current font.
            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);

            //adjust the width of the column that contains the row header cells 
            //if necessary
            if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);

            //this brush will be used to draw the row number string on the
            //row header cell using the system's current ControlText color
            Brush b = SystemBrushes.ControlText;

            //draw the row number string on the current row header cell using
            //the brush defined above and the DataGridView's default font
            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));

            base.OnRowPostPaint(e);
        }
    }
}
